package com.daoliuhe.scaffold.web.controller.admin;

import com.daoliuhe.scaffold.model.Role;
import com.daoliuhe.scaffold.model.RoleAuthoritiesRelation;
import com.daoliuhe.scaffold.model.UserGroupRole;
import com.daoliuhe.scaffold.model.UserRoleRelation;
import com.daoliuhe.scaffold.service.RoleAuthoritiesRelationService;
import com.daoliuhe.scaffold.service.RoleService;
import com.daoliuhe.scaffold.service.UserGroupRoleService;
import com.daoliuhe.scaffold.service.UserRoleRelationService;
import com.daoliuhe.scaffold.web.dto.RoleDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;

@Controller
@RequestMapping("/role")
public class RoleController {
    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    RoleService roleService;

    @Autowired
    UserRoleRelationService userRoleRelationService;

    @Autowired
    UserGroupRoleService userGroupRoleService;

    @Autowired
    RoleAuthoritiesRelationService roleAuthoritiesRelationService;

    @RequestMapping("/list")
    public ModelAndView list(Role role) {
        logger.info("list,role:{}", role);
        ModelAndView mav = new ModelAndView("admin/role/list");
        mav.addAllObjects(roleService.getPageData(role));
        mav.addObject("entity", role);
        return mav;
    }

    @RequestMapping("/edit")
    public ModelAndView edit(Role role, @RequestParam(required = false) String tab, RoleDto roleDto) {
        logger.info("edit,role:{},tab:{},roleDto{}", role, tab, roleDto);
        ModelAndView mav = new ModelAndView("admin/role/edit");
        if (!StringUtils.isEmpty(role.getId())) {
            Role r = roleService.getById(role.getId());
            r.setPage(role.getPage());
            r.setRows(role.getRows());
            mav.addObject("entity", r);

            // 关联的用户分页
            UserRoleRelation ur = new UserRoleRelation();
            ur.setRoleId(role.getId());
            ur.setPage(roleDto.getuPage());
            ur.setRows(roleDto.getuRows());
            mav.addObject("userMap", userRoleRelationService.getUserPageData(ur));
            roleDto.setuPage(ur.getPage());

            // 关联的用户组分页
            UserGroupRole ugr = new UserGroupRole();
            ugr.setRoleId(new BigDecimal(role.getId()));
            ugr.setPage(roleDto.getUgPage());
            ugr.setRows(roleDto.getUgRows());
            mav.addObject("userGroupMap", userGroupRoleService.getUserGroupPageData(ugr));
            roleDto.setUgPage(ugr.getPage());

            // 权限
            RoleAuthoritiesRelation auth = new RoleAuthoritiesRelation();
            auth.setRoleId(role.getId());
            auth.setPage(roleDto.getaPage());
            auth.setRows(roleDto.getaRows());
            mav.addObject("authMap", roleAuthoritiesRelationService.getAuthPageData(auth));
            roleDto.setaPage(auth.getPage());

            mav.addObject("roleDto", roleDto);
            mav.addObject("tab", tab);
        }
        mav.addObject("param", role);
        return mav;
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role) {
        logger.info("saveOrUpdate,role:{}", role);
        roleService.saveOrUpdate(role);
        return "redirect:/role/list";
    }

    @RequestMapping("/data")
    @ResponseBody
    public Object data(Role role) {
        logger.info("data,role:{}", role);
        return roleService.getPageData(role);
    }

    @RequestMapping("/vName")
    @ResponseBody
    public Object vName(String name, String id) {
        logger.info("vName,name:{},id:{}", name, id);
        return roleService.isRightName(name, id);
    }
}
